package com.example.project.controller;

import com.example.project.model.Feedback;
import com.example.project.service.FeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/feedback")
public class FeedbackController {

    @Autowired
    private FeedbackService feedbackService;

    // 获取所有反馈
    @GetMapping
    public List<Feedback> getAllFeedbacks() {
        return feedbackService.getAllFeedbacks();
    }

    // 添加反馈
    @PostMapping
    public ResponseEntity<String> addFeedback(@RequestBody Feedback feedback) {
        boolean result = feedbackService.addFeedback(feedback);
        if (result) {
            return ResponseEntity.status(HttpStatus.CREATED).body("反馈添加成功");
        } else {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("反馈添加失败：用户不存在");
        }
    }

    // 删除反馈
    @DeleteMapping("/{id}")
    public ResponseEntity<String> deleteFeedback(@PathVariable int id) {
        boolean result = feedbackService.deleteFeedback(id);
        if (result) {
            return ResponseEntity.ok("反馈删除成功");
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("反馈删除失败：反馈未找到");
        }
    }

    // 获取反馈详情
    @GetMapping("/{id}")
    public ResponseEntity<Feedback> getFeedbackDetails(@PathVariable int id) {
        Feedback feedback = feedbackService.getFeedbackById(id);
        if (feedback != null) {
            return ResponseEntity.ok(feedback);
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
    }
}